package com.xy.xymail.product.service.impl;

import com.xy.xymail.product.entity.AttrGroupEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.common.utils.PageUtils;
import com.xy.common.utils.Query;

import com.xy.xymail.product.dao.CategoryDao;
import com.xy.xymail.product.entity.CategoryEntity;
import com.xy.xymail.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //1.查出所有分类
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //2.组装成父子结构
        //2.1先查到父id属性为0 的分类,即是所有的一级分类
        List<CategoryEntity> categoryEntityListWithTree = categoryEntities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() == 0L
        ).map(menu -> {
            menu.setChildren(getMenuChildrens(menu, categoryEntities));
            return menu;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());


        return categoryEntityListWithTree;
    }

    //递归查找所有菜单的子菜单
    public List<CategoryEntity> getMenuChildrens(CategoryEntity current, List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            //找出父id等于目前菜单id的所有id
            //因为是long类型，所以当产犊超过128之后，用 == 会变成false
            return categoryEntity.getParentCid().equals(current.getCatId());
        }).map(categoryEntity -> {
            //递归再查找子菜单
            categoryEntity.setChildren(getMenuChildrens(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());

        return children;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //TODO  1、检查当前删除的菜单，是否被别的地方引用
        //逻辑删除
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] getCatelogPath(Long catelogId) {

        List<Long> catelogPath = new ArrayList<>();
        //根据菜单id得到所有的关联id的数组
        catelogPath = getParentPath(catelogId, catelogPath);
        Collections.reverse(catelogPath);
        return catelogPath.toArray(new Long[catelogPath.size()]);
    }

    private List<Long> getParentPath(Long catelogId,List<Long> catelogPath) {
        //先将id放进list
        catelogPath.add(catelogId);
        CategoryEntity byId = categoryService.getById(catelogId);

        if (byId.getParentCid()!=0){
            //存在父id
            Long parentCid = byId.getParentCid();

            getParentPath(parentCid,catelogPath);
        }
        return catelogPath;
    }

}
